package edu.shiep.sunyu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;

import edu.shiep.sunyu.model.Bus;
import edu.shiep.sunyu.model.DBClient;
import edu.shiep.sunyu.model.DBConstants;
import edu.shiep.sunyu.model.Database;
import edu.shiep.sunyu.model.Table;
import edu.shiep.sunyu.model.User;

public class BookingLoginServlet extends HttpServlet {

	private static final long serialVersionUID = 2589121138111015311L;
	
	private DBClient _client = null;
	
	public void init(ServletConfig config) throws ServletException {
	   super.init(config);
	   
	   _client = new DBClient();
	   if (!_client.connect()) {
		   System.out.println("Can't connect to database!!!");
	   }
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// reading the user input
		String user_id = request.getParameter("user_id");
		String user_password = request.getParameter("user_password");
		String user_type = request.getParameter("user_type");
		
		// Generate session
		
		if (user_type != "guest") {
			String generatedPassword = Utility.getMD5Password(user_password);
	
			Database db = _client.getDB(DBConstants.Const_Database_Name);
			
			Table table = db.getTable(DBConstants.Const_Table_User_Name);
			
			BasicDBObject query = new BasicDBObject();
			query.put(User.Field_Id, user_id);
			query.put(User.Field_Password, generatedPassword);
			
			DBCursor cursor = table.query(query);
			
			if (cursor.count() == 0) {
				request.setAttribute("msg",	"The user who you provided is not exist. Please try again.");
				
				RequestDispatcher view = request.getRequestDispatcher("BookingLogin.jsp");
				view.forward(request, response);
				return;
			}
			
			// set session for user
			HttpSession session = request.getSession();
			session.setAttribute("user", user_id);
			session.setAttribute("is_guest", false);
		} else {
			// set session for guest
			HttpSession session = request.getSession();
			session.setAttribute("user", "");
			session.setAttribute("is_guest", true);
		}
		
		// Get bus
		Database db = _client.getDB(DBConstants.Const_Database_Name);
		
		Table table = db.getTable(DBConstants.Const_Table_Bus_Name);
		
		DBCursor cursor = table.query();
		
		List lstBus = new ArrayList();
		
		while (cursor.hasNext()) { 
        	BasicDBObject doc = (BasicDBObject)cursor.next();
        	Bus bus = new Bus();
        	bus.deserialize(doc);
        	
        	lstBus.add(bus.getId());
        	System.out.println(doc);
        }
		
		request.setAttribute("buses", lstBus);
		
		RequestDispatcher view = request.getRequestDispatcher("BookingSelectBus.jsp");
		view.forward(request, response);
	}  
	
}
